'use strict'

class image{
    constructor(){
        this.param = '/:name'
    }

    async get (c){
        let imgname = c.param.name;

        await c.helper.pipe(`${c.service.imagePath}/images/${imgname}`, c.reply)
    }

    async post (c){
        //根据name获取文件对象
        let f = c.getFile('image')

        if(!f){
            return c.status(400).send('image not found')
        }

        //根据传入的原始文件生成唯一文件名，主要使用f.filename的扩展名
        let fname = c.helper.makeName(f.filename)

        //移动文件到指定位置
        await c.moveFile(f, `${c.service.imagePath}/images/${fname}`)
        c.send(fname)
        }
}

module.exports = image